import { createRouter, createWebHistory } from 'vue-router'
import { isLoggedIn } from '@/utils/auth'
import Layout from '@/components/Layout.vue'
import Dashboard from '@/views/Dashboard.vue'
import Login from '@/views/Login.vue'
import NotFound from '@/views/NotFound.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: () => {
        return isLoggedIn() ? '/dashboard' : '/login'
      },
    },
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: { requiresAuth: false },
    },
    {
      path: '/test',
      name: 'Test',
      component: () => import('@/views/TestView.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '/',
      component: Layout,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: Dashboard,
        },
        // 园区管理
        {
          path: 'park/student',
          name: 'StudentManage',
          component: () => import('@/views/park/StudentManage.vue'),
        },
        {
          path: 'park/enterprise',
          name: 'EnterpriseManage',
          component: () => import('@/views/park/EnterpriseManage.vue'),
        },
        // 行车管理
        {
          path: 'industry/area',
          name: 'AreaManage',
          component: () => import('@/views/industry/AreaManage.vue'),
        },
        {
          path: 'industry/monthly',
          name: 'MonthlyManage',
          component: () => import('@/views/industry/MonthlyManage.vue'),
        },
        {
          path: 'industry/temp',
          name: 'TempManage',
          component: () => import('@/views/industry/TempManage.vue'),
        },
        {
          path: 'industry/rule',
          name: 'RuleManage',
          component: () => import('@/views/industry/RuleManage.vue'),
        },
        // 物业费管理
        {
          path: 'property',
          name: 'PropertyManage',
          component: () => import('@/views/PropertyManage.vue'),
        },
        // 一体化管理
        {
          path: 'integration/manage',
          name: 'IntegrationManage',
          component: () => import('@/views/integration/IntegrationManage.vue'),
        },
        {
          path: 'integration/notice',
          name: 'NoticeManage',
          component: () => import('@/views/integration/NoticeManage.vue'),
        },
        // 抽奖管理
        {
          path: 'lottery/category',
          name: 'CategoryManage',
          component: () => import('@/views/lottery/CategoryManage.vue'),
        },
        {
          path: 'lottery/prizes',
          name: 'PrizeManage',
          component: () => import('@/views/lottery/PrizeManage.vue'),
        },
        {
          path: 'lottery/draws',
          name: 'DrawManage',
          component: () => import('@/views/lottery/DrawManage.vue'),
        },
        // 系统管理
        {
          path: 'system/employee',
          name: 'EmployeeManage',
          component: () => import('@/views/system/EmployeeManage.vue'),
        },
        {
          path: 'system/role',
          name: 'RoleManage',
          component: () => import('@/views/system/RoleManage.vue'),
        },
      ],
    },
    // 404页面
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: NotFound,
      meta: { requiresAuth: false },
    },
  ],
})

// 简化的路由守卫
router.beforeEach((to, from, next) => {
  try {
    const isAuthenticated = isLoggedIn()

    // 如果访问登录页且已登录，跳转到仪表盘
    if (to.path === '/login' && isAuthenticated) {
      next('/dashboard')
      return
    }

    // 如果访问需要认证的页面但未登录，跳转到登录页
    if (to.meta?.requiresAuth !== false && !isAuthenticated) {
      next('/login')
      return
    }

    // 正常访问
    next()
  } catch (error) {
    console.error('路由守卫错误:', error)
    // 出错时跳转到登录页
    next('/login')
  }
})

export default router
